package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import Projektarbete.ContactTabListFiller;
import Projektarbete.Group;
import Projektarbete.Restrictions;
import Projektarbete.User;

import javax.swing.*;

/**
 * 
 * @author Calle
 */
public class GroupSettings extends JPanel implements MouseListener {

	private Group group;
	private JButton addMemberButton;
	private JCheckBox chatCheck;
	private JCheckBox downloadCheck;
	private JCheckBox modCheck;
	private JLabel jLabel1;
	private JScrollPane jScrollPane1;
	private JSeparator jSeparator1;
	private JSeparator jSeparator2;
	private JButton leaveButton;
	private JLabel memberRestLabel;
	private JLabel miscLabel;
	private JButton removeButton;
	private JButton removeMemberButton;
	private JCheckBox removeCheck;
	private JTextField renameInput;
	private JLabel renameLabel;
	private JButton saveRestButton;
	private JButton saveNameButton;
	private JLabel settingsLabel;
	private JCheckBox uploadCheck;
	private JList userList;
	private Font mainFont;
	private MainFrame mf;
	private ArrayList<User> members;
	private boolean isOwner;
	private User user;

	public GroupSettings(Group group, User user, MainFrame mf) {
		this.group = group;
		this.mf = mf;
		this.user = user;
		if(group.isOwner(user))
			isOwner = true;
		else
			isOwner = false;
		initComponents();
	}

	@SuppressWarnings("unchecked")
	private void initComponents() {

		jScrollPane1 = new JScrollPane();
		userList = new JList();
		jLabel1 = new JLabel();
		settingsLabel = new JLabel("Settings");
		modCheck = new JCheckBox("Moderator");
		jSeparator1 = new JSeparator();
		downloadCheck = new JCheckBox("Download Files");
		uploadCheck = new JCheckBox("Upload Files");
		removeCheck = new JCheckBox("Remove Files");
		saveRestButton = new JButton("Save");
		removeMemberButton = new JButton("Remove member");
		removeButton = new JButton("Remove Group");
		renameInput = new JTextField();
		renameLabel = new JLabel("Rename Group");
		chatCheck = new JCheckBox("Write in Chat");
		memberRestLabel = new JLabel("Member Restrictions");
		jSeparator2 = new JSeparator();
		miscLabel = new JLabel("Miscellaneous");
		leaveButton = new JButton("Leave Group");
		addMemberButton = new JButton("Add Member");
		saveNameButton = new JButton("Save");
		mainFont = new Font("Aliquam", 0, 11);

		if(!isOwner){
			downloadCheck.setEnabled(false);
			uploadCheck.setEnabled(false);
			removeCheck.setEnabled(false);
			chatCheck.setEnabled(false);
			saveRestButton.setEnabled(false);
			saveNameButton.setEnabled(false);
			removeMemberButton.setEnabled(false);
			removeButton.setEnabled(false);
			modCheck.setEnabled(false);
		}
		members = new ArrayList<User>();
		for(int i=0;i<group.size();i++){
			members.add(group.getUser(i));
		}
		userList.setFont(new Font("Georgia", 0, 14));
		userList.setModel(new AbstractListModel() {
			public int getSize() {
				return members.size();
			}

			public Object getElementAt(int i) {
				if(group.isOwner(members.get(i))){
					return members.get(i).getUserName()+" (owner)";
				}
				return members.get(i).getUserName();
			}
		});
		userList.addMouseListener(this);
		jScrollPane1.setViewportView(userList);
	
		userList.setSelectedIndex(0);
		getRestrictions();
		
		jLabel1.setFont(new Font("Aliquam", 0, 11));
		settingsLabel.setFont(new Font("Aliquam", 0, 24));
		jSeparator1.setForeground(new Color(0, 0, 0));
		downloadCheck.setFont(mainFont);
		uploadCheck.setFont(mainFont);
		removeCheck.setFont(mainFont);
		removeMemberButton.setFont(mainFont);
		saveRestButton.setFont(mainFont);
		removeButton.setFont(mainFont);
		renameLabel.setFont(mainFont);
		chatCheck.setFont(mainFont);
		leaveButton.setFont(mainFont);
		addMemberButton.setFont(mainFont);
		saveNameButton.setFont(mainFont);
		modCheck.setFont(mainFont);
		memberRestLabel.setFont(new Font("Aliquam", 0, 14));
		miscLabel.setFont(new Font("Aliquam", 0, 14));
		jSeparator2.setForeground(new Color(0, 0, 0));

		saveRestButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				saveRest(evt);
			}

			private void saveRest(ActionEvent evt) {
				Restrictions rest = new Restrictions(members.get(userList.getSelectedIndex()).getU_id(),
						downloadCheck.isSelected(), uploadCheck.isSelected(), removeCheck.isSelected(),
						chatCheck.isSelected());
				group.setRestrictions(rest);
				new ContactTabListFiller().updateGroup(group);
				getRestrictions();
			}
		});

		saveNameButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				changeName(evt);
			}
			public void changeName(ActionEvent evt) {
				if (!renameInput.getText().equals("")) {
					group.setName(renameInput.getText());
					new ContactTabListFiller().updateGroup(group);
				}
			}
		});
		
		addMemberButton.addActionListener(new java.awt.event.ActionListener(){
			public void actionPerformed(ActionEvent evt){
				addNewMember(evt);
			}

			private void addNewMember(ActionEvent evt) {
				new AddGroupMember(mf, true,group,user).setVisible(true);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(removeMemberButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 83, Short.MAX_VALUE)
                                .addComponent(saveNameButton)
                                .addGap(18, 18, 18))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(uploadCheck)
                                    .addComponent(downloadCheck)
                                    .addComponent(removeCheck)
                                    .addComponent(chatCheck)
                                    .addComponent(modCheck))
                                .addContainerGap())))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(miscLabel)
                            .addComponent(renameLabel)
                            .addComponent(memberRestLabel))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSeparator1)
                            .addComponent(jSeparator2)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addGap(0, 0, Short.MAX_VALUE)
                                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(settingsLabel)
                                        .addGap(0, 224, Short.MAX_VALUE)))
                                .addGap(213, 213, 213))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(renameInput, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(saveNameButton))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(removeButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(addMemberButton)
                                .addGap(92, 92, 92)
                                .addComponent(leaveButton)))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(settingsLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(memberRestLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(downloadCheck)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(uploadCheck)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(removeCheck)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(chatCheck)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(modCheck)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 116, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(removeMemberButton)
                            .addComponent(saveNameButton)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(miscLabel)
                .addGap(49, 49, 49)
                .addComponent(renameLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(saveNameButton)
                    .addComponent(renameInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(leaveButton)
                    .addComponent(addMemberButton)
                    .addComponent(removeButton))
                .addGap(12, 12, 12))
        );
    }

	@Override
	public void mouseClicked(MouseEvent arg0) {
		getRestrictions();
	}

	private void getRestrictions() {
		Restrictions rest = group.getRestrictions(members.get(userList.getSelectedIndex()));
		if(rest.isDownload()){
			downloadCheck.setSelected(true);
		} else{
			downloadCheck.setSelected(false);
		}
		if(rest.isRemove()){
			removeCheck.setSelected(true);
		} else{
			removeCheck.setSelected(false);
		}
		if(rest.isUpload()){
			uploadCheck.setSelected(true);
		} else {
			uploadCheck.setSelected(false);
		}
		if(rest.isWrite()){
			chatCheck.setSelected(true);
		} else {
			chatCheck.setSelected(false);
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
	}
}
